package com.paisheng.justlife.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.paisheng.justlife.R;
import com.paisheng.justlife.appliction.MyApplication;
import com.paisheng.justlife.bean.ComBean;
import com.paisheng.justlife.utils.MyFunc;
import com.paisheng.justlife.utils.RegularExpression;
import com.paisheng.justlife.utils.SerialPortUtils;
import com.paisheng.justlife.utils.ToastUtil;

import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 手机号码验证页面
 */

public class VerifyActivity extends Activity implements View.OnClickListener {

    private TextView tvBack, tvTime;
    private EditText phoneEt;
    private Button phoneBnt, one, two, three, four, five, six, seven, eight, nine, zero, delAll, del;
    private Intent intent;
    Timer timer = new Timer();
    private SerialPortUtils serialPortUtils;
    private String sMsg;
    private int recLen = 60;
    MyApplication context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify);
//        serialListener();
        initView();
        registerClick();
        tvTime.setText(recLen + "");
//        Intent intent = getIntent();
//        String metal = intent.getStringExtra("openClass");
//        System.out.println(metal + "---");


    }

    private void serialListener() { //监听接口返回值
        MyApplication content = (MyApplication) getApplicationContext();
         serialPortUtils = content.serialPortUtils;
//        serialPortUtils = SerialPortUtils.getSerialPortUtils();
        this.serialPortUtils.setOnDataReceiveListener(new SerialPortUtils.OnDataReceiveListener() {
            @Override
            public void onDataReceive(ComBean ComRecData) {
                sMsg = MyFunc.ByteArrToHex(ComRecData.bRec);
                System.out.println(sMsg);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        timer.schedule(task, 1000, 1000);
    }


    private void initView() {
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvTime = (TextView) findViewById(R.id.tv_time);
        phoneEt = (EditText) findViewById(R.id.phone_et);
        phoneBnt = (Button) findViewById(R.id.phone_bnt);
        one = (Button) findViewById(R.id.one);
        two = (Button) findViewById(R.id.two);
        three = (Button) findViewById(R.id.three);
        four = (Button) findViewById(R.id.four);
        five = (Button) findViewById(R.id.five);
        six = (Button) findViewById(R.id.six);
        seven = (Button) findViewById(R.id.seven);
        eight = (Button) findViewById(R.id.eight);
        nine = (Button) findViewById(R.id.nine);
        zero = (Button) findViewById(R.id.zero);
        delAll = (Button) findViewById(R.id.del_all);
        del = (Button) findViewById(R.id.del);
        phoneEt.setSelection(phoneEt.getText().length());
//        phoneEt.setKeyListener(null);
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            phoneEt.setInputType(InputType.TYPE_NULL);
        } else {
            VerifyActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setSoftInputShownOnFocus;
                setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                setSoftInputShownOnFocus.setAccessible(true);
                setSoftInputShownOnFocus.invoke(phoneEt, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void registerClick() {
        tvBack.setOnClickListener(this);
        phoneBnt.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        delAll.setOnClickListener(this);
        del.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.phone_bnt:
                String phoneNum = phoneEt.getText().toString();
                if (RegularExpression.isMobileNO(phoneNum)) {
                    intent = getIntent();
                    String metal = intent.getStringExtra("openClass");
                    intent = new Intent(this, BinOpenActivity.class);
                    intent.putExtra("openClass", metal);
                    intent.putExtra("phone", phoneEt.getText().toString());
                    startActivity(intent);
                } else {
                    ToastUtil.showLong(this, "手机号码格式不正确！");
                }

                break;
            case R.id.one:
                phoneEt.append("1");
                break;
            case R.id.two:
                phoneEt.append("2");
                break;
            case R.id.three:
                phoneEt.append("3");
                break;
            case R.id.four:
                phoneEt.append("4");
                break;
            case R.id.five:
                phoneEt.append("5");
                break;
            case R.id.six:
                phoneEt.append("6");
                break;
            case R.id.seven:
                phoneEt.append("7");
                break;
            case R.id.eight:
                phoneEt.append("8");
                break;
            case R.id.nine:
                phoneEt.append("9");
                break;
            case R.id.zero:
                phoneEt.append("0");
                break;
            case R.id.del:
                if (phoneEt.getText().toString().length() == 0) {
                    break;
                }
                int index = phoneEt.getSelectionStart();
                phoneEt.getText().delete(index - 1, index);
                break;
            case R.id.del_all:
                phoneEt.setText("");
                break;
        }
    }

    public TimerTask task = new TimerTask() {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    recLen--;
                    tvTime.setText("" + recLen);
                    if (recLen <= 0) {
                        startActivity(new Intent(VerifyActivity.this, MainActivity.class));
                        timer.cancel();
                    }
                }
            });

        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        timer.cancel();
        ToastUtil.cancelToast();
        finish();
    }


}
